// Copyright (c) 2024 Deng Zicheng
// "CarController" is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
import Prompt from '@system.prompt';
import Router from '@system.router';

const ABILITY_TYPE_EXTERNAL = 0;
const ACTION_SYNC = 0;
const ACTION_MESSAGE_CODE_CALL = 1001;
// const ACTION_MESSAGE_CODE_SUBSCRIBE = 1005;
// const ACTION_MESSAGE_CODE_UNSUBSCRIBE = 1006;
export  class MessageData{
    constructor(option,speed=0,degree=0,destination_X,destination_Y,address='',code='') {
        this.option=option;//操作
        this.speed=speed;//速度
        this.degree=degree;//角度
        this.destination_X=destination_X;//位置
        this.destination_Y=destination_Y;//位置
        this.address=address;//地址
        this.code=code;//地址
    }
}
const TYPE_SCAN=0;
const TYPE_DEVICES=1;
const TYPE_CONNECT=2;

export default {
    data: {
        devices:'',//蓝牙设备信息列表
        index:null,
        intervalID:null,
        timeoutID:null,
        scanning:false
    },
    onInit(){
    },
    onDestroy(){
        clearInterval(this.intervalID);
        clearTimeout(this.timeoutID);
    },
    /**
     * 开始扫描设备
     */
    async scan(){
        if (this.scanning) {
            Prompt.showToast({message:"扫描中请稍后！"});
            return;
        }
        await this.actionJava(new MessageData("scan"));
        this.scanning=true;
        if (this.intervalID!=null) {
            clearInterval(this.intervalID)
        }
        this.intervalID=setInterval(this.updateDevice,2000)//更新信息
        this.timeoutID=setTimeout(this.scanTimeout,30000);//超时时间
    },

    /**
     * 扫描超时
     */
    scanTimeout(){
        this.scanning=false;
        clearInterval(this.intervalID);
        Prompt.showToast({message:"扫描超时！"});
        this.actionJava(new MessageData("scanStop"));
    },

    /**
     * 更新设备信息
     */
    async updateDevice(){
        await this.actionJava(new MessageData("devices"));
    },
    /**
     * 连接设备
     * @param e
     */
    async connect(e){
        let  address=e.target.attr.tid
        let data=new MessageData("connect");
        data.address=address;
        await this.actionJava(data);
    },
    /**
     * 重连接
     */
    reconnect(){
        let data=new MessageData();
        data.option="connect";
        this.actionJava(data);
    },
    /**
     * action初始化
     * @returns
     */
    initAction(){
        var action = {};
        action.bundleName = 'com.sinan.myapplication';
        action.abilityName = 'com.sinan.myapplication.bleBTServiceAbility';
        return action;
    },
    /**
     * 调用java初始化
     * @param data
     * @returns
     */
    initActionCall(data){
        var actionData = data;
        var action=this.initAction();
        action.messageCode = ACTION_MESSAGE_CODE_CALL;
        action.data = actionData;
        action.abilityType = ABILITY_TYPE_EXTERNAL;
        action.syncOption = ACTION_SYNC;
        return action;
    },

    /**
     * 调用java实现
     * @param data
     */
    async actionJava(data) {
        var result = await FeatureAbility.callAbility(this.initActionCall(data));
        console.log(JSON.stringify(result));
        result=JSON.parse(result);
        if (result.code == 1){
            Prompt.showToast({message:"操作失败！"});
            return;
        }else if (result.type==TYPE_SCAN){
            Prompt.showToast({message:"扫描中！"});
        }else if (result.type==TYPE_DEVICES){
            this.devices=result.devices;
        }else if (result.type==TYPE_CONNECT){
            Router.back();
        }
    },

}